package org.cuatrovientos.erp.controller;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.validation.Valid;

import org.apache.log4j.Logger;
import org.cuatrovientos.erp.business.SupplierBOInterface;
import org.cuatrovientos.erp.model.Supplier;
import org.springframework.stereotype.Controller;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.beans.factory.annotation.Autowired;

/**
 * Controller class form Supplier management
 * 
 * @author VB
 */
@Controller
public class SupplierController {

	// Get log4j logger
	private static final Logger logger = Logger.getLogger(SupplierController.class);

	@Autowired
	private SupplierBOInterface supplierBO;

	/**
	 * default constructor
	 */
	public SupplierController() {
		logger.info("Supplier BEAN instantiated");
	}

	/**
	 * handles default /supplier or /supplier/list request
	 * 
	 * @param model
	 * @return the name of the view to show
	 * @RequestMapping({"/supplier","/supplier/list" )
	 */
	@RequestMapping({ "/supplier", "/supplier/list" })
	public ModelAndView supplierDefault() {
		logger.info("default");

		Map<String, Object> model = new HashMap<String, Object>();

		List<Supplier> supplierList = supplierBO.selectAll();
		model.put("suppliers", supplierList);
		return new ModelAndView("SupplierList", model);
	}

	/**
	 * handles /supplier/search request, redirected to default page
	 * 
	 * @param model
	 * @return the name of the view to show
	 * @RequestMapping({"/supplier/search" )
	 */
	@RequestMapping(method = RequestMethod.POST, value = { "/supplier/search" })
	public ModelAndView search(@RequestParam(value = "name", defaultValue = "", required = true) String name) {
		logger.info("search");

		Map<String, Object> model = new HashMap<String, Object>();

		List<Supplier> supplierList = supplierBO.search(name);
		model.put("suppliers", supplierList);

		return new ModelAndView("SupplierList", model);
	}

	/**
	 * handles delete supplier, and redirect to supplier default
	 * 
	 * @param model
	 * @return the name of the view to show
	 * @RequestMapping({"/supplier","/supplier/list" )
	 */
	@RequestMapping(method = RequestMethod.GET, value = { "/supplier/delete" })
	public String deleteSupplier(@RequestParam(value = "id", defaultValue = "", required = true) int id) {
		logger.info("delete " + id);

		supplierBO.deleteById(id);
		return "redirect:/supplier";
	}

	/**
	 * handles new supplier request. In the jsp we are using spring's form:form
	 * tags Spring expects a model called by default 'command' or any name we
	 * set to it: in this case "supplier"
	 * 
	 * @return the name of the view to show
	 * @RequestMapping({"/supplier/new" )
	 */
	@RequestMapping(method = RequestMethod.GET, value = { "/supplier/new" })
	public ModelAndView newSupplier() {
		logger.info("new supplier ");
		Supplier supplier = new Supplier();

		return new ModelAndView("NewSupplier", "supplier", supplier);
	}

	/**
	 * handles new supplier request. In the jsp we are using spring's form:form
	 * tags Spring expects a model called by default 'command' or any name we
	 * set to it.
	 * 
	 * @return the name of the view to show
	 * @RequestMapping({"/supplier/new" )
	 */
	@RequestMapping(method = RequestMethod.POST, value = { "/supplier/save" })
	public String newSupplier(@Valid Supplier supplier,BindingResult bindingResult) {
		logger.info("new supplier " + supplier.toString());

		// In case of validation errors, go back to form
		if (bindingResult.hasErrors()) {
			Map<String, Object> model = new HashMap<String, Object>();
			model.put("supplier", supplier);
			return "NewSupplier";
		}

		// If data is ok, insert and go on
		supplierBO.insert(supplier);
		return "redirect:/supplier";
	}

	/**
	 * handles delete supplier, and redirect to supplier default
	 * 
	 * @param model
	 * @return the name of the view to show
	 * @RequestMapping({"/supplier","/supplier/list" )
	 */
	@RequestMapping(method = RequestMethod.GET, value = { "/supplier/detail" })
	public ModelAndView supplierDetail(@RequestParam(value = "id", defaultValue = "", required = true) int id) {
		logger.info("Detail " + id);
		Map<String, Object> model = new HashMap<String, Object>();
		Supplier supplier = supplierBO.selectById(id);
		model.put("supplier", supplier);

		return new ModelAndView("SupplierDetail", model);
	}

	/**
	 * handles supplier update, first part: register loaded
	 * 
	 * @param model
	 * @return the name of the view to show
	 * @RequestMapping({"/supplier/update" )
	 */
	@RequestMapping(method = RequestMethod.GET, value = { "/supplier/update" })
	public ModelAndView update(@RequestParam(value = "id", defaultValue = "", required = true) int id) {
		logger.info("update " + id);
		Map<String, Object> model = new HashMap<String, Object>();
		Supplier supplier = supplierBO.selectById(id);
		model.put("supplier", supplier);
		return new ModelAndView("UpdateSupplier", model);

	}

	/**
	 * handles save update supplier request. In the jsp we are using spring's
	 * form:form tags Spring expects a model called by default 'command' or any
	 * name we set to it.
	 * 
	 * @return the name of the view to show
	 * @RequestMapping({"/supplier/saveUpdate" )
	 */
	@RequestMapping(method = RequestMethod.POST, value = { "/supplier/saveupdate" })
	public String saveUpdate(@Valid Supplier supplier,BindingResult bindingResult) {
		logger.info("update supplier " + supplier.toString());

		// In case of validation errors, go back to form
		if (bindingResult.hasErrors()) {
			Map<String, Object> model = new HashMap<String, Object>();
			model.put("supplier", supplier);
			return "UpdateSupplier";
		}

		supplierBO.update(supplier);
		return "redirect:/supplier";
	}
}
